package com.yuanfeng.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.constant.CommonType;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import com.yuanfeng.goods.entity.GoodsArticleLikeEntity;
import com.yuanfeng.goods.entity.GoodsArticleRemarkEntity;
import com.yuanfeng.goods.mapper.GoodsArticleInfoMapper;
import com.yuanfeng.goods.mapper.GoodsArticleLikeMapper;
import com.yuanfeng.goods.mapper.GoodsArticleRemarkMapper;
import com.yuanfeng.goods.service.GoodsArticleLikeService;
import com.yuanfeng.goods.vo.CommunityLikeEntityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName GoodsArticleLikeServiceImpl
 * @Description  社区心得点赞表
 * @Author cyp
 * @Date 2023-07-18 14:51:33
 * @Version 1.0
 **/
@Service
public class GoodsArticleLikeServiceImpl extends ServiceImpl<GoodsArticleLikeMapper, GoodsArticleLikeEntity>
        implements GoodsArticleLikeService {

    @Autowired
    private GoodsArticleInfoMapper goodsArticleInfoMapper;
    @Autowired
    private GoodsArticleRemarkMapper goodsArticleRemarkMapper;

    @Override
    public ResponseResult likeArticle(Map<Object, Object> param) {
        UserInfoVO ui = ParseTokenUtils.getUserInfoByToken(param.get("token").toString());
        if(null == ui){
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID.getCode(),BizCodeEnume.TOKEN_IS_INVALID.getMsg());
        }
        param.put("likeUser",ui.getUserId());
        param.put("userId",ui.getUserId());
        int likeFlag = (int) param.get("likeFlag");
        if (1 == likeFlag) {
            Integer count = this.baseMapper.getArticleIsLiked(param);
            if(count != null){
                return ResponseResult.success("已点赞");
            }
            if(param.get("likeUser").equals(param.get("likedUser"))){
                param.put("isRead", CommonType.commonStatus.YES.getCode());
            }else {
                param.put("isRead", CommonType.commonStatus.NO.getCode());
            }
            //点赞
            this.baseMapper.insertLike(param);
        } else {
            //取消
            this.baseMapper.deleteLike(param);
        }
        //点赞类型:1-心得;2-评论
        int likeType = (int) param.get("likeType");
        if(1 == likeType) {
             //对心得点赞
             goodsArticleInfoMapper.updateLikeNum(param);

        }else{
             //对评论点赞
            goodsArticleRemarkMapper.updateLikeNumForComments(param);

        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult queryLikeList(Map<String, Object> param) {
        UserInfoVO ui = ParseTokenUtils.getUserInfoByToken(param.get("token").toString());
        if(null == ui){
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID.getCode(),BizCodeEnume.TOKEN_IS_INVALID.getMsg());
        }else{
            param.put("userId",ui.getUserId());
            IPage<CommunityLikeEntityVO> page = this.baseMapper.getLikeList(new Query<CommunityLikeEntityVO>().getPage(param),param);
            List<CommunityLikeEntityVO> likeList = page.getRecords();
            for(CommunityLikeEntityVO communityLikeEntity:likeList){
                Map<String,String> likeUserInfo = goodsArticleInfoMapper.getUserInfo(communityLikeEntity.getLikeUser());
                communityLikeEntity.setLikeUserName(likeUserInfo.get("userName"));
                communityLikeEntity.setLikeUserAvatar(likeUserInfo.get("userAvatar"));
                Integer likeType = communityLikeEntity.getLikeType();
                //点赞心得
                if(1 == likeType){
                    String articleId = communityLikeEntity.getArticleId();
                    String articleTitle = goodsArticleInfoMapper.getArticleTitle(articleId);
                    communityLikeEntity.setArticleTitle(articleTitle);
                    //点赞评论
                }else if(2 == likeType){
                    String remarkId = communityLikeEntity.getLikedId();
                    String remarkContent = goodsArticleRemarkMapper.getRemarkContent(remarkId);
                    communityLikeEntity.setRemarkContent(remarkContent);
                }
            }
            this.baseMapper.update(null,new LambdaUpdateWrapper<GoodsArticleLikeEntity>()
                    .set(GoodsArticleLikeEntity::getIsRead,CommonType.commonStatus.YES.getCode())
                    .eq(GoodsArticleLikeEntity::getLikedUser,ui.getUserId()));
            page.setRecords(likeList);
            return ResponseResult.success(PageUtils.getPage(page));
        }
    }

    @Override
    public ResponseResult queryLikeAndRemarkCount(Map<String, Object> param) {
        UserInfoVO ui = ParseTokenUtils.getUserInfoByToken(param.get("token").toString());
        if(null == ui){
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }else{
            Map<String, Object> map = new HashMap<>();
            Integer likeCount = this.baseMapper.selectCount(new LambdaQueryWrapper<GoodsArticleLikeEntity>()
                    .eq(GoodsArticleLikeEntity::getIsRead,CommonType.commonStatus.NO.getCode())
                    .eq(GoodsArticleLikeEntity::getLikedUser,ui.getUserId()));
            Integer remarkCount = goodsArticleRemarkMapper.selectCount(new LambdaQueryWrapper<GoodsArticleRemarkEntity>()
                    .eq(GoodsArticleRemarkEntity::getIsRead,CommonType.commonStatus.NO.getCode())
                    .eq(GoodsArticleRemarkEntity::getRemarkedUser,ui.getUserId()));
            map.put("likeCount",likeCount);
            map.put("remarkCount",remarkCount);
            return ResponseResult.success(map);
        }
    }
}
